import Unit from "./unit";
import Angle from "../utils/Angle";

export interface UnitBoxData {
  /**
   * box type
   * - `0`: 默认
   * - `1`: 加速
   * - `2`: 减速
   */
  type: number;
  position: number;
}

export interface UnitBoxDataLink extends UnitBoxData {
  next: UnitBoxDataLink;
}

export default class UnitBox extends Unit {

  public saveData: UnitBoxData[] = [];
  public data: UnitBoxData[] = [];
  public angle: Angle = new Angle(180);
  public targetAngle: Angle = new Angle(180);

  public setData(data: UnitBoxData[]) {
    this.saveData = [...data];
    this.data = data;
    this.targetAngle.Angle = Angle.FixedAngle(this.angle.Angle + this.data[0].position + 180);
  }

  /** @deprecate */
  public drawExcute_rect(context2D: CanvasRenderingContext2D) {
    const width = 30;
    const height = 20;
    const width_2 = width / 2;
    const height_2 = height / 2;
    const width_4 = width / 4;
    const data = this.data;
    context2D.fillStyle = "green";
    context2D.strokeStyle = "#424242";
    context2D.lineWidth = 2
    context2D.rotate(new Angle(this.angle.Angle + 180).Value);
    for (let i = 0; i < data.length; i++) {
      this.game.drawSave((context2D) => {
        context2D.translate(-width_2, -height_2)
        context2D.fillRect(0, 0, width - width_4, height);
        context2D.strokeRect(0, 0, width - width_4, height);
      });
      context2D.rotate(new Angle(data[i].position + 180).Value);
      this.game.drawSave((context2D) => {
        context2D.translate(-width_2, -height_2)
        context2D.fillRect(width_4, 0, width - width_4, height);
        context2D.strokeRect(width_4, 0, width - width_4, height);
      });
      context2D.translate(width , 0);
    }
  }

  public drawExcute(context2D: CanvasRenderingContext2D) {
    const width = 40;
    const height = 20;
    const width_2 = width / 2;
    const data = this.data;
    context2D.fillStyle = "green";
    context2D.strokeStyle = "#424242";
    context2D.lineWidth = height;
    context2D.rotate(new Angle(this.angle.Angle + 180).Value);
    for (let i = 0; i < data.length; i++) {
      const box = data[i];
      const angle = box.position;
      if (box.type === 0) {
        // nop
      } else if (box.type === 1) {
        this.game.drawSave(() => {
          context2D.fillStyle = "red";
          context2D.strokeStyle = "red";
          context2D.font = "15px blod sans-serif";
          context2D.textAlign = "center";
          context2D.textBaseline = "middle";
          context2D.lineWidth = 1;
          context2D.fillText("F", 0, 2);
          context2D.strokeText("F", 0, 2);
        });
      } else if (box.type === 2) {
        this.game.drawSave(() => {
          context2D.fillStyle = "red";
          context2D.strokeStyle = "red";
          context2D.font = "15px blod sans-serif";
          context2D.textAlign = "center";
          context2D.textBaseline = "middle";
          context2D.lineWidth = 1;
          context2D.fillText("S", 0, 2);
          context2D.strokeText("S", 0, 2);
        });
      }
      context2D.lineJoin = angle < 90 ? "round" : "miter";
      context2D.beginPath();
      context2D.moveTo(-width_2, 0);
      context2D.lineTo(0, 0);
      context2D.rotate(new Angle(angle + 180).Value);
      context2D.lineTo(width_2 - 1, 0);
      context2D.stroke();
      context2D.translate(width , 0);
    }
  }

  public next() {
    this.data.shift();
    const currentBox = this.data[0];
    if (currentBox.type === 0) {
      // nop
    } else if (currentBox.type === 1) {
      this.game.setting.speed += 1;
    } else if (currentBox.type === 2) {
      this.game.setting.speed -= 1;
    }
    this.angle.Angle = this.targetAngle.Angle;
    this.targetAngle.Angle = Angle.FixedAngle(this.angle.Angle + currentBox.position + 180);
  }

  public rePlay() {
    this.data = [...this.saveData];
    this.angle = new Angle(180);
    this.targetAngle.Angle = Angle.FixedAngle(this.angle.Angle + this.data[0].position + 180);
  }

}
